package com.zyt.springboot.CalculationPI;

import java.util.Random;

public class PI2 {//蒙特卡洛方法计算圆周率
    public static void main(String[] args) {
        final int iterations = 1000000; // 迭代次数
        Random rand = new Random();
        int insideCircle = 0;

        for (int i = 0; i < iterations; i++) {
            double x = rand.nextDouble() * 2 - 1; // x坐标在-1到1之间
            double y = rand.nextDouble() * 2 - 1; // y坐标在-1到1之间

            if (x * x + y * y <= 1) {
                insideCircle++;
            }
        }

        double pi = (4.0 * insideCircle) / iterations;
        System.out.println("Estimated Pi: " + pi);
    }
}
